<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="配置名称:" prop="name">
          <el-input v-model="formData.name" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="回传开关:" prop="isOpen">
          <el-select v-model="formData.isOpen" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in switchOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="回传转化方式:" prop="mediaSourceType">
          <el-select v-model="formData.mediaSourceType" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in mediaSourceTypeOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="仅回传新用户:" prop="callbackNewUser">
          <el-select v-model="formData.callbackNewUser" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in isnoOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="回传类型策略:" prop="callbackType">
          <el-select v-model="formData.callbackType" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in callbackTypeOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="Y 单位 元:" prop="callbackTypeMoney">
          <el-input v-model.number="formData.callbackTypeMoney" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="回传部分订单设置:" prop="callbackOpOpen">
          <el-select v-model="formData.callbackOpOpen" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in switchOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="回传部分订单-自定先回传笔数:" prop="callbackOpNum">
          <el-input v-model.number="formData.callbackOpNum" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="每日先回传的订单是否按金额过滤:" prop="isOpFilterMoney">
          <el-select v-model="formData.isOpFilterMoney" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in isnoOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="回传部分订单-策略:" prop="callbackOpType">
          <el-select v-model="formData.callbackOpType" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in callbackOpTypeOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="百分比回传-比率:" prop="callbackOpPercentage">
          <el-select v-model="formData.callbackOpPercentage" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in callbackOpPercentageOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="循环比例回传-比例:" prop="callbackOpRatio">
          <el-select v-model="formData.callbackOpRatio" placeholder="请选择" :clearable="true">
              <el-option value=""     label="--未选择--" />
              <el-option value="1:1"  label="--1 : 1--"/>
              <el-option value="2:1" label="--2 : 1--" />
              <el-option value="3:1" label="--3 : 1--" />
              <el-option value="4:1" label="--4 : 1--" />
              <el-option value="5:1" label="--5 : 1--" />
              <el-option value="6:1" label="--6 : 1--" />
              <el-option value="7:1" label="--7 : 1--" />
              <el-option value="8:1" label="--8 : 1--" />
              <el-option value="9:1" label="--9 : 1--" />
          </el-select>
       </el-form-item>
        <el-form-item label="循环规则:" prop="callbackOpRule">
          <el-select v-model="formData.callbackOpRule" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in callbackOpRuleOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="回传上限（用户维度回传总数量）:" prop="callbackOpMaxByUser">
          <el-select v-model="formData.callbackOpMaxByUser" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in isReportOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="VIP充值金额上报:" prop="callbackVipOpen">
          <el-select v-model="formData.callbackVipOpen" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in isReportOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="开启ROI全量上报:" prop="callbackRoiOpen">
          <el-select v-model="formData.callbackRoiOpen" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in isReportOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="创建人id:" prop="createUid">
          <el-input v-model.number="formData.createUid" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="createTime字段:" prop="createTime">
          <el-date-picker v-model="formData.createTime" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item label="更新用户id:" prop="updateUid">
          <el-input v-model.number="formData.updateUid" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="updateTime字段:" prop="updateTime">
          <el-date-picker v-model="formData.updateTime" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createTConversionRulesZ,
  updateTConversionRulesZ,
  findTConversionRulesZ
} from '@/api/tConversionRulesZ'

defineOptions({
    name: 'TConversionRulesZForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'

const route = useRoute()
const router = useRouter()

const type = ref('')
const isnoOptions = ref([])
const callbackTypeOptions = ref([])
const callbackOpPercentageOptions = ref([])
const callbackOpRuleOptions = ref([])
const switchOptions = ref([])
const mediaSourceTypeOptions = ref([])
const callbackOpTypeOptions = ref([])
const isReportOptions = ref([])
const formData = ref({
            name: '',
            isOpen: undefined,
            mediaSourceType: undefined,
            callbackNewUser: undefined,
            callbackType: undefined,
            callbackTypeMoney: 0,
            callbackOpOpen: undefined,
            callbackOpNum: 0,
            isOpFilterMoney: undefined,
            callbackOpType: undefined,
            callbackOpPercentage: undefined,
            callbackOpRatio: '',
            callbackOpRule: undefined,
            callbackOpMaxByUser: undefined,
            callbackVipOpen: undefined,
            callbackRoiOpen: undefined,
            createUid: 0,
            createTime: new Date(),
            updateUid: 0,
            updateTime: new Date(),
        })
// 验证规则
const rule = reactive({
               name : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findTConversionRulesZ({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data.retConversionRulesZ
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    isnoOptions.value = await getDictFunc('isno')
    callbackTypeOptions.value = await getDictFunc('callbackType')
    callbackOpPercentageOptions.value = await getDictFunc('callbackOpPercentage')
    callbackOpRuleOptions.value = await getDictFunc('callbackOpRule')
    switchOptions.value = await getDictFunc('switch')
    mediaSourceTypeOptions.value = await getDictFunc('mediaSourceType')
    callbackOpTypeOptions.value = await getDictFunc('callbackOpType')
    isReportOptions.value = await getDictFunc('isReport')
}

init()
// 保存按钮
const save = async() => {
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return
            let res
           switch (type.value) {
             case 'create':
               res = await createTConversionRulesZ(formData.value)
               break
             case 'update':
               res = await updateTConversionRulesZ(formData.value)
               break
             default:
               res = await createTConversionRulesZ(formData.value)
               break
           }
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
